// pages/address/address.js
const apiurl = require('../../config.js').apiurl;
import {
  Pulibc
} from '../../models/public.js';
const pulibc = new Pulibc();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allAddress: [],
    changeBool: 0,
    showState:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(options)
    // if (wx.getStorageSync('changeBool')) {
    //   wx.removeStorageSync('changeBool');
    // } 
    if (options.changeBool) {
      wx.setStorageSync('changeBool', options.changeBool);
    }
    
  },
  // 编辑地址
  bindEdit(e) {
    let id = e.currentTarget.dataset.id;
    // 存储编辑地址id
    this.data.allAddress.map(item => {
      if (item.address_id == id) {
        if (wx.getStorageSync('city_id')) {
          wx.removeStorageSync('city_id')
        }
        wx.setStorageSync('city_id', {
          provincial_id: item.provincial_id,
          city_id: item.city_id,
          area_id: item.area_id
        })
      }
    })
    pulibc.navigateUrl('/pages/address-edit/address-edit?address_id=' + id)
    // wx.redirectTo({
    //   url:"/pages/address-edit/address-edit?address_id=" +id
    // })
  },
  // 删除地址
  deleteAddress(e) {
    console.log(e)
    let allAddress = this.data.allAddress;
    let index = e.currentTarget.dataset.index;
    let address_id = e.currentTarget.dataset.id;
    let url = apiurl + '/address/addressdel';
    let params = {};
    params.key = wx.getStorageSync('key');
    params.address_id = address_id;
    wx.request({
      url: url,
      data: params,
      header: {
        'Content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: res => {
        console.log(res)
        //  刷新列表数据
        this.getAddress();
        pulibc.showToast('删除成功', 'none')
      }
    })
  },
  toPayOrder(e) {
    // cart_id存在，才跳转
    if (!wx.getStorageSync('changeBool')) {
      return;
    }
    if (wx.getStorageSync('changeBool')) {
      let address_id = e.currentTarget.dataset.id;
      console.log("address_id", address_id)
      // pulibc.navigateUrl('../to-pay-order/to-pay-order?address_id=' + address_id)
      wx.redirectTo({
        url: '../to-pay-order/to-pay-order?address_id=' + address_id
      })
      wx.removeStorageSync('changeBool');
    }
    console.log("changeBool", wx.getStorageSync('changeBool'))
  },
  getAddress() {
    let that = this;
    let url = apiurl + '/address/addresslist';
    let params = {};
    params.key = wx.getStorageSync('key');
    params.page = 1;
    wx.request({
      url: url,
      data: params,
      success(res) {
        console.log(res);
        if (res.data.code == 200) {
          let allAddress = res.data.datas.list;
          that.setData({
            allAddress
          })
        } else if (res.data.datas.error == '请登录'){
          wx.navigateTo({
            url: "../register/register"
          })
        }

      }
    })
  },
  bindAddressTap(e) {
    pulibc.navigateUrl('/pages/address-add/address-add');
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (wx.getStorageSync("key") == false) {
      wx.navigateTo({
        url: "../register/register"
      })
    }else {
      this.getAddress();
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})